import cv2
import os


class PhotoCapture:
    def __init__(self, save_folder: str):
        os.makedirs(save_folder, exist_ok=True)
        self.save_folder = save_folder
        self.cap = cv2.VideoCapture(0)

    def capture_photos(self, num_photos: int):
        photos = []
        for i in range(num_photos):
            ret, frame = self.cap.read()
            photo_path = os.path.join(self.save_folder, f"photo_{i + 1}.jpg")
            cv2.imwrite(photo_path, frame)
            photos.append(photo_path)
        return photos[-1]

    def __del__(self):
        self.cap.release()


if __name__ == "__main__":
    num_photos = 5
    save_folder = "photos"
    photo_capture = PhotoCapture(save_folder)
    last_photo = photo_capture.capture_photos(num_photos)
    img = cv2.imread(last_photo, 0)
    cv2.imshow('Last Photo', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
